//
//  WeaterView.m
//  PlayerFix
//
//  Created by dllo on 15/11/11.
//  Copyright © 2015年 DAT. All rights reserved.
//

#import "WeaterView.h"
#import "WeaterModel.h"
#import "ChineseToPinyin.h"
@implementation WeaterView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
    
}



- (void)createSubViews
{
//    self.backgroundColor = [UIColor redColor];
    
    self.weaterArray = [NSMutableArray array];
    // 开始定位
    
    [self.maneger startUpdatingLocation];
    // 运行就定位 先判断版本
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        [self.maneger requestWhenInUseAuthorization];
    }

    // 图标
    self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 40, 50, 50)];
//    self.myImageView.backgroundColor = [UIColor redColor];
    [self addSubview:self.myImageView];
    // 日期
    self.temperatureLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 45, 100, 20)];
    [self addSubview:self.temperatureLabel];
    // 天气
    self.weatherLabel = [[UILabel alloc] initWithFrame:CGRectMake(190, 45, 100, 20)];
    [self addSubview:self.weatherLabel];
    // 地点
    self.cityidLabel = [[UILabel alloc] initWithFrame:CGRectMake(190, 70, 100, 20)];
    [self addSubview:self.cityidLabel];
    // 温度
    self.daysLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 70, 100, 20)];
    [self addSubview:self.daysLabel];
    
    
    
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    CLLocation *location = [locations lastObject];
    CLLocationCoordinate2D coordinate = location.coordinate;
    NSLog(@"经度:%.2f 纬度:%.2f",coordinate.latitude, coordinate.longitude);
    
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        // 如果有错误或者解析出来的地址数量是0直接返回
        if (placemarks.count == 0 || error) {
            NSLog(@"空");
            return;
        }
        NSString *weaid;
        CLPlacemark *placemark = [placemarks firstObject];
        if (placemark.locality) {
//            NSLog(@"%@",placemark.locality);
            weaid = placemark.locality;
        }else{
//            NSLog(@"%@",placemark.administrativeArea);
            weaid = placemark.administrativeArea;
        }
        [self getAfnInfo:weaid];

        // 停止定位
        [self.maneger stopUpdatingLocation];
    }];
    
}

- (void)getAfnInfo:(NSString *)weaid
{
//    NSLog(@"weeeee= %@",weaid);
    NSString *weaidPinyin = [ChineseToPinyin pinyinFromChiniseString:weaid];
//    NSLog(@"weaid = %@",weaidPinyin);
    NSString *wea;
    if ([weaidPinyin hasSuffix:@"SHI"]) {
        wea = [weaidPinyin stringByReplacingOccurrencesOfString:@"SHI" withString:@""];
        wea = [wea lowercaseString];
    }
    
    NSString *url = [NSString stringWithFormat:@"http://api.k780.com:88/?app=weather.future&weaid=%@&appkey=16303&sign=08b52a9b4c4ecaafd33f0aafac633ad1&format=json",wea];
//    NSLog(@"url = %@",url);
    NSLog(@"weaid = %@",wea);

    [DATAsyncGetAFNBlock GetAFNURLWithString:url andBody:nil andCookie:nil resultBlock:^(id result) {
        
        NSDictionary *bigDic = result;
        NSArray *array = [bigDic objectForKey:@"result"];
        for (NSDictionary *dic in array) {
            WeaterModel *weaterModel = [[WeaterModel alloc] init];
            [weaterModel setValuesForKeysWithDictionary:dic];
            [_weaterArray addObject:weaterModel];
        }
        WeaterModel *weaterModel = [_weaterArray firstObject];
        if ([weaterModel.weather isEqualToString:@"云"]) {
            self.myImageView.image = [UIImage imageNamed:@"iconfont-duoyun.png"];
        }
        if ([weaterModel.weather isEqualToString:@"阴"]) {
            self.myImageView.image = [UIImage imageNamed:@"iconfont-yin.png"];
        }
        if ([weaterModel.weather isEqualToString:@"雪"]) {
            self.myImageView.image = [UIImage imageNamed:@"iconfont-zhongxue.png"];
        }
        if ([weaterModel.weather isEqualToString:@"雨"]) {
            self.myImageView.image = [UIImage imageNamed:@"iconfont-zhongyu.png"];
        }else{
            self.myImageView.image = [UIImage imageNamed:@"iconfont-qing.png"];
        }
       
        
        
        self.weatherLabel.text = weaterModel.weather;
        self.weatherLabel.font = [UIFont systemFontOfSize:13];
        self.weatherLabel.textColor = [UIColor whiteColor];
        self.temperatureLabel.text = weaterModel.temperature;
        self.temperatureLabel.font = [UIFont systemFontOfSize:13];
        self.temperatureLabel.textColor = [UIColor whiteColor];
        self.daysLabel.text = weaterModel.days;
        self.daysLabel.font = [UIFont systemFontOfSize:13];
        self.daysLabel.textColor = [UIColor whiteColor];
        self.cityidLabel.text = weaterModel.citynm;
        self.cityidLabel.font = [UIFont systemFontOfSize:13];
        self.cityidLabel.textColor = [UIColor whiteColor];

        
    } errorBlock:^(NSError *error) {
        
        NSLog(@"weaterError = %@",error);
        
    }];
}

#pragma mark -懒加载
- (CLLocationManager *)maneger
{
    if (_maneger == nil) {
        // 1. 创建定位管理者
        _maneger = [[CLLocationManager alloc] init];
        // 2. 设置代理
        _maneger.delegate = self;
        // 3. 位置间隔之后重新定位
        _maneger.distanceFilter = 10;
        // 4. 定位的精确度
        _maneger.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    }
    return _maneger;
}


@end
